//
//  RVMyOrderListController.swift
//  AirTravel
//
//  Created by apple on 2021/11/16.
//

import UIKit
import HandyJSON
enum RVMyrOrderType {
    case all//全部
    case topay//代付款
    case diliver//代发货
    case accept//待收货
    case comment//待评价
    case finish//已完成
}


class RVMyOrderListController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    
    var type: RVMyrOrderType
    
    var data: RVMyOrderData!
    
    init(type: RVMyrOrderType){
        
        self.type = type
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        confUI()
    }
    
    func confUI(){
        tableView.register(UINib(nibName: "RVMyOrderListCell", bundle: nil), forCellReuseIdentifier: "myorder_list")
        tableView.rowHeight = 115
        tableView.reloadData()
        getDatas()
    }
    
    func getDatas(){
        var parameters: [String: Any] = [String: Any]()
        var orderType: String = ""
        switch type {
        case .all:
            orderType = "0"
        case .topay:
            orderType = "10"
        case .diliver:
            orderType = "20"
        case .accept:
            orderType = "30"
        case .comment:
            orderType = "40"
        case .finish:
            orderType = "50"
        }
        parameters["type"] = orderType
        YProgressHUD.show()
        NetWorkRequest(API1.getMyOrder(parameters: parameters)) { json in
            
            YProgressHUD.dismiss()
            guard let data = JSONDeserializer<RVMyOrderData>.deserializeFrom(dict: json.dictionaryObject) else{
                return
            }
            self.data = data
            self.tableView.reloadData()
            
        } failed: { msg in
            YProgressHUD.dismiss()

        } errorResult: {
            YProgressHUD.dismiss()
        }

    }

}

extension RVMyOrderListController: UITableViewDelegate, UITableViewDataSource{
    
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 45
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 110
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        guard let header = Bundle.main.loadNibNamed("RVMyOrderHeader", owner: nil, options: nil)?.last as? RVMyOrderHeader else {
            return nil
        }
        guard let dt = self.data else {
            return nil
        }
        let order = dt.data[section]
        
        header.orderNOLabel.text = "订单号：" + order.order_sn
        header.statusLabel.text = "无状态"
        return header
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        guard let footer = Bundle.main.loadNibNamed("RVMyOrderFooter", owner: nil, options: nil)?.last as? RVMyOrderFooter else {
            return nil
        }
        guard let dt = self.data else {
            return nil
        }
        let order = dt.data[section]
        
        footer.countLabel.text = "共\(order.orderinfo_count)件商品"
        footer.priceLabel.text = "合计：¥\(order.pay_price)"
        return footer
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        guard let _ = data else {
            return 0
        }
        return data.data.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        guard let _ = data else {
            return 0
        }
        return data.data[section].orderinfo.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "myorder_list", for: indexPath) as! RVMyOrderListCell
        guard let _ = data else {
            return cell
        }
        
        let list = data.data[indexPath.section].orderinfo[indexPath.row]
        cell.nameLabel.text = list.title
        cell.priceLabel.text = "¥" + list.order_price
        let url = URL(string: list.image)
        cell.imgView.kf.setImage(with: url)
        return cell
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}

